import {createRouter, createWebHistory} from "vue-router";
import {useUserStore} from "@/stores/user";

// @ts-ignore

const routers=[
    {
        path: '/',
        name: 'Home',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/Home.vue'),
        children:[
            {
                path:'allgz',
                name:'AllGz',
                meta: { public: false},
                // @ts-ignore
                component: () => import('@/components/AllGuanZhu.vue'),
            },
            {
                path:'notice',
                name:'Notice',
                meta: { public: false},
                // @ts-ignore
                component: () => import('@/components/Notice.vue'),
            },
            {
                path:'',
                name:'Post',
                // @ts-ignore
                component: () => import('@/components/Post.vue'),
            },
            {
                path:'poster',
                name:'poster',
                // @ts-ignore
                component: () => import('@/components/Poster.vue'),
            },
            {
                path:'hot',
                name:'hot',
                // @ts-ignore
                component: () => import('@/components/wzHotSearch.vue'),
            },

        ]
    },
    {
        path: '/login',
        name: 'Login',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/Login.vue'),
    },


]
const router = createRouter({
    history: createWebHistory(),
    routes: routers
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore()

    // 如果不是公开路由且未登录
    if (!to.meta.public && !userStore.isLoggedIn) {
        next({ name: 'Login'})
    } else {
        next()
    }
})
export default router